define('sendPhoneCodeView',['common'],function(utils){
	var sendPhoneCodeView = (function(data,suc){
		utils.ajax(utils.getWebUrl('verify/sendPhoneCode'),
			data,
			'POST',
		function(res){
			suc(res);
		});	
	});
	return sendPhoneCodeView;
})

define('isIdCard',['common'],function(utils){
	var isIdCard = (function(data,suc){
		var data = data;
		$.ajax({
            url: utils.getWebUrl('verify/isIdCard'),
            type: 'POST',
            data: data, 
            dataType: 'json',
            headers: {
               'authorization': 'Bearer '+utils.userToken()
            },
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(res.code === "10000"){
                    suc(res)
                }else{
                    utils.appAlert('该身份证号码已被其它用户使用，请更换身份证号码后进行实名认证');
       				return;
                }
            },
            error: function(res) {
                utils.appAlert('系统出现了错误,系统管理员正在修复');
            }
        });
	})
	return isIdCard;
})

define([
	'vue',
	'config',
	'common',
	'sendPhoneCodeView',
	'isIdCard'
	],function(Vue,config,utils,sendPhoneCodeView,isIdCard){
	'use strict';

	var certificationView=new Vue({
		el:'#app',
		data:{
			realName:'',
			carId:'',
			cardNo:'',
			banks:'',
			branch:'',

			phone:'',
			phoneCode_cer:'',
			secondOfDelay : 120000 ,
			secondLeftOfDelay : 0,

			backType:'',
			submitBtn: false
		},
		created:function(){
			utils.userToken();
			var _this = this;
			_this.backType = utils.getQueryVariable('type')*1;
			window.addEventListener('popstate',function() {	
	       		utils.setData('certification','');
	       		switch(this.backType){
					case 1:
					  window.location.href="Settings.html";
					  break;
					case 2:
					  history.back();
					  break;
					case 3:
					  window.location.href="withdraw.html";
					  break;
					case 4:
					  window.location.href="withdraw_firm.html";
					  break;
					default:
					  window.location.href="userInfo.html";
				}
		    })
			utils.ajax(utils.getWebUrl('user/personal'),null,'POST',function(res){
				_this.phone = res.data.phone;
			},function(){
				$('.DataLoading').show();
			},function(){
				$('.DataLoading').hide();
			})
		},
		filters:{
			showPhone:function(phone){
	            var start = phone.toString().slice(0,3);
	            var end = phone.toString().slice(phone.toString().length-3,phone.toString().length);
	            return (start + '*****' + end)
	        }
		},
		methods:{
			back:function(){
				utils.setData('certification','');
				switch(this.backType){
					case 1:
					  window.location.href="Settings.html";
					  break;
					case 2:
					  history.back();
					  break;
					case 3:
					  window.location.href="withdraw.html";
					  break;
					case 4:
					  window.location.href="withdraw_firm.html";
					  break;
					default:
					  window.location.href="userInfo.html";
				}
				
			},
			checkId:function() {
	        	this.carId = this.carId.replace(/[^0-9|(x|X)$]/g,'');
	        },
			inputPhone:function(event){
				//console.log(this.phone)
				if(this.phone.length === 11){
					if(this.phone !== this.userPhone){
						this.fadeIn();
					}else{
						this.fadeOut();
					}
				}
			},
			getVeryCode:function(){
				var _this =this;
				if($.trim(_this.realName) === ""){
					utils.appAlert('请输入真实姓名');
					return;
				}
				if($.trim(_this.carId) === ""){
					utils.appAlert('请输入身份证号码');
					return;
				}
			    if(!(/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/).test(_this.carId)){
				     utils.appAlert('请输入正确的身份证号码');
				     return;
			    }
				isIdCard({
					idCard: _this.carId
				},function(res){
					sendPhoneCodeView({
						phone:_this.phone,
						codeType:6,
						smsType:1
					},function(res){
						utils.appAlert('请查收你的短信',function(){
				        	_this.sendVerifyCodeTimer();
				        	$(".verification").attr('disabled','true');
				        });	        
					})
				},function(){
					$('.DataLoading').show();
				},function(){
					$('.DataLoading').hide();
				})
				
			},
			sendVerifyCodeTimer:function(){
				var $time_debounce = $('.verification');
				var _this=this;
				_this.setTimeToDefaut = function(secondsOfInterval) {
                	this.secondLeftOfDelay = secondsOfInterval;
            	};

				var secondsOfInterval = this.secondOfDelay;

				var intervalID = setInterval(function() {
	                secondsOfInterval = secondsOfInterval - 1000;
	                $time_debounce.html(" " + secondsOfInterval / 1000 + " 秒 ")
	                if (secondsOfInterval <= 0) {
	                    clearInterval(intervalID);
	                    $(".verification").removeAttr("disabled");
	                    $time_debounce.html("获取验证码");
	                }
	                _this.setTimeToDefaut(secondsOfInterval);
	            }, 1000);
			},
			/*getvoiceMessage:function(){
				var _this=this;
				sendPhoneCodeView({
					phone: _this.phone,
					codeType: 6,
					smsType: 2
				},function(res){
					utils.appAlert('你正在使用语音获取验证码<br/>请保持手机通畅');
				})
			},*/
			certification_toNext:function(){
				var type = utils.getQueryVariable('type');
				//console.log(type)
				var _this = this;
				if($.trim(_this.realName) === ""){
					utils.appAlert('请输入真实姓名');
					return;
				}
				if($.trim(_this.carId) === ""){
					utils.appAlert('请输入身份证号码');
					return;
				}
				if(!(/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/).test(_this.carId)){
					utils.appAlert('请输入正确的身份证号码');
					return;
				}

				isIdCard({
					idCard: _this.carId
				},function(res){
					if($.trim(_this.phoneCode_cer) === ""){
						utils.appAlert('请输入验证码');
						return;
					}
					
					if (_this.submitBtn) {
						return;
					}

					var data = {
						realName: _this.realName,
						idCard: _this.carId,
						phoneCode: _this.phoneCode_cer
					}
					_this.submitBtn = true;	
					$.ajax({
		                url: utils.getWebUrl('verify/realName'),
		                type: 'POST',
		                data: data,
		                dataType: 'json',
		                headers: {
		                   'authorization': 'Bearer '+ utils.userToken()
		                },
		                xhrFields: {
		                    withCredentials: true
		                },
		                crossDomain: true,
		                success: function(res) {
		                    if(res.code === "10000"){
		                    	 window.location.href = "AddBank_realName.html?type=" + type;
		                    	 var list = {
		                    	 	realName: _this.realName,
									idCard: _this.carId
		                    	 }
		                    	 utils.setData('certification',list)
		                    }else{
		                    	_this.submitBtn = false;
		                        utils.appAlert(res.msg);
		                    }
		                },
		                beforeSend:function(){
		                	$('.msg').show();
		                },
		                complete:function(){
							$('.msg').hide();
						},
		                error: function(res){
		                	_this.submitBtn = false;
		                    utils.appAlert(res.msg);
		                }
		            });
				})
			}
		}
	},function(){
		$('.DataLoading').show();
		
	},function(){
		$('.DataLoading').hide();
	})

	return certificationView
});